package com.audaque.springboot.foshanupload.schedule.component;

/**
 * @author zgb
 * @desc ...
 * @date 2022-04-17 20:25:40
 */

import cn.hutool.core.bean.BeanUtil;
import com.audaque.springboot.foshanupload.schedule.abstra.AbstractDynamicSchedule;
import com.audaque.springboot.foshanupload.schedule.impl.DynamicScheduleImpl;


import com.audaque.springboot.foshanupload.schedule.mapper.DynamicScheduleMpgtMapper;
import com.audaque.springboot.foshanupload.schedule.model.domain.DynamicScheduleMpgtDo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;
@ConditionalOnProperty(value = "task.dynamicSchedule.switchYn",havingValue = "true",matchIfMissing = false)

@Component
public class DynamicScheduleProvider extends AbstractDynamicSchedule<DynamicScheduleImpl> {

    @Autowired
    private DynamicScheduleMpgtMapper dynamicScheduleMpgtMapper;


    public volatile List<DynamicScheduleImpl> taskInfoList = null;

    @Override
    protected List<DynamicScheduleImpl> listTaskInfo() {

        DynamicScheduleMpgtDo dynamicScheduleMpgtDo = new DynamicScheduleMpgtDo();
        QueryWrapper queryWrapper = new QueryWrapper(dynamicScheduleMpgtDo);
        List<DynamicScheduleMpgtDo> list = dynamicScheduleMpgtMapper.selectList(queryWrapper);
        List<DynamicScheduleImpl> taskInfoList = BeanUtil.copyToList(list, DynamicScheduleImpl.class,null);
        return taskInfoList;
    }



    /**
     * 做具体的任务逻辑
     */
    protected void doProcess(DynamicScheduleImpl taskInfo) {
        final String reference = taskInfo.getReference();
        final String[] split = reference.split("#");
        if (split.length != 2) {
            return;
        }

        try {
            final Class<?> clazz = Class.forName(split[0]);
            final Method method = clazz.getMethod(split[1]);
            method.invoke(clazz.newInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }






}
